import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

/*
 ID: ning yusui
 LANG: JAVA
 TASK: inflate
 */

public class inflate {
    Scanner in;
    PrintWriter out;

    public inflate() throws IOException {
        in = new Scanner(new File(this.getClass().getName() + ".in"));
        out = new PrintWriter(new BufferedWriter(new FileWriter(this.getClass().getName() +
                ".out")));
    }

    private void work() {
        int m=in.nextInt();
        int[] scores= new int[m+1];
        int n=in.nextInt();
        for(int i=0; i<n; i++){
            int score=in.nextInt();
            int minute=in.nextInt();
            for(int j=0; j<= m - minute; j++){
               if(scores[j+minute] < scores[j]+score){
                  scores[j+minute] = scores[j]+score;
               }
            }
        }
        int ans=0;
        for(int i=0; i<=m; i++){
           if(ans<scores[i]){
               ans=scores[i];
           }
        }
        out.println(ans);
        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new inflate().work();
    }
}
